<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    * {
        margin: 0 auto;
    }

    #box {
        padding: 20px;
        background: plum;
    }
</style>

<body>
    <div id="box"></div>
    <button id="btn1">执行计算任务</button>
    <button id="btn2">更改背景颜色</button>
    <script>
        let box = document.getElementById("box")
        let btn1 = document.getElementById("btn1")
        let btn2 = document.getElementById("btn2")
        let num = 99999;
        let value = 0
        function calc(deadline) {
            while (num > 0 && deadline.timeRemaining()>1) {
                value = Math.random() > 0.5 ? Math.random() : Math.random()
                console.log(value)
                num--
            }
            requestIdleCallback(calc)
        }
        btn1.onclick = function () {
            // calc()
            requestIdleCallback(calc)
        }
        btn2.onclick = function () {
            box.style.background = "yellow"
        }

    </script>
</body>

</html>